//
//  FCCustomerRelationViewController.m
//  customerRelation_Example
//
//  Created by 陈文斌 on 2019/2/15.
//  Copyright © 2019 64912907. All rights reserved.
//

#import "FCCustomerRelationViewController.h"
#import "FCAddRelationViewController.h"
#import "FCSearchRelationViewController.h"
#import "FCRelationContentViewController.h"
#import "FCCustomerRelationRequest.h"
#import "FCRelationTypeListModel.h"
#import "MJExtension.h"
#import "CustomerRelationHeader.h"
#import "UIViewController+Permiss.h"

@interface FCCustomerRelationViewController ()
@property (nonatomic, strong) UIButton *searchBtn;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIImageView *navBarHairlineImageView;

@end

@implementation FCCustomerRelationViewController
- (UIBarButtonItem *)itemWithColor:(UIColor *)color highColor:(UIColor *)highColor target:(id)target action:(SEL)action title:(NSString *)title{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button setTitleColor:highColor forState:UIControlStateHighlighted];
//    [button sizeToFit];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return  [[UIBarButtonItem alloc] initWithCustomView:button];
}

// 初始化UI
- (void)setupUI{
    self.title = customerRelationLocaChanged(@"关系管理", @"关系管理");
    self.view.backgroundColor = FCBackColor;
    
    // 导航栏添加上下游按钮
    UIBarButtonItem *addBarBut = [self itemWithColor:FCBlueTxtColor highColor:FCBlueTxtColor target:self action:@selector(addAction) title:customerRelationLocaChanged(@"添加上下游", @"添加上下游")];
    self.navigationItem.rightBarButtonItems = @[addBarBut];
    
    UIView *searchView = [[UIView alloc] init];
    [self.view addSubview:searchView];
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.right.mas_equalTo(0);
        make.height.mas_equalTo(48);
    }];
    searchView.backgroundColor = [UIColor whiteColor];
    _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchView addSubview:_searchBtn];
    [_searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(30);
    }];
    _searchBtn.titleLabel.font = font13;
    _searchBtn.backgroundColor = FCBackColor;
    _searchBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    _searchBtn.layer.cornerRadius = 4;
    _searchBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_searchBtn setImage:customerRelationImageName(@"nav_ic_search_g") forState:UIControlStateNormal];
    [_searchBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 12, 0, 0)];
    [_searchBtn setTitle:customerRelationLocaChanged(@"global_general_search", @"搜索") forState:UIControlStateNormal];
    [_searchBtn setTitleColor:FCGrayTxtPlaceColor forState:UIControlStateNormal];
    [_searchBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 18, 0, 0)];
    [_searchBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.contentView = [UIView new];
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(searchView.mas_bottom).mas_equalTo(10);
        make.left.and.right.mas_equalTo(0);
        make.bottom.mas_equalTo(FCTabbarSafeBottomMargin);
    }];
}

#pragma mark - data
- (void)getRelationType{
    [FCCustomerRelationRequest getCrmSupplyRelationTypeWithArgs:@{@"enable": @(YES)} success:^(id resultObj) {
        FCRelationTypeListModel *relationTypesModel = [FCRelationTypeListModel mj_objectWithKeyValues:resultObj[@"data"][@"crmSupplyRelationTypes"]];
        
        FCRelationContentViewController *relationContentVC = [[FCRelationContentViewController alloc] init];
        [self addChildViewController:relationContentVC];
        [relationContentVC didMoveToParentViewController:self];
        
        relationContentVC.relationRelationTypes = relationTypesModel.items;
        [self.contentView addSubview:relationContentVC.view];
        
    } failure:^(NSError *error) {
        XMGLog(@"%@", error);
    }];
}

// 搜索上下游关系
- (IBAction)searchAction {
    FCSearchRelationViewController *searchRelationVC = [[FCSearchRelationViewController alloc] init];
    [self.navigationController pushViewController:searchRelationVC animated:NO];
}

// 添加上下游关系
- (void)addAction{
    XMGLog(@"添加");
    WJCWeakSelf
    [self checkPermissionWithCode:@"Crm:Relations.View" andReturnBlock:^(FCPermissSubCellModel *permission) {
        if (permission.isAllowedByCurrIdentity) {
            if ([FCEmployeeModel isBusinessWithNotice:YES andPushController:weakSelf]) {
                FCAddRelationViewController *addRelattionVC = [[FCAddRelationViewController alloc] init];
                [weakSelf.navigationController pushViewController:addRelattionVC animated:YES];
            }
        } else {
            weakSelf.permissionCode=@"Crm:Relations.View";
            [weakSelf checkPermissionandReturnBlock:^(FCPermissSubCellModel * _Nonnull permission) {
                
            }];
            return;
        }
    }];
    
    return ;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    _navBarHairlineImageView.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getRelationType];
    [self setupUI];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _navBarHairlineImageView.hidden = NO;
}

#pragma mark - 获取导航栏黑线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    
    return nil;
}

-(void)loadNewData{
    [[NSNotificationCenter defaultCenter] postNotificationName:CrmSupplyRelationChanged object:nil];
}

@end
